﻿@page "/"
@inject ICatalogService CatalogService

<div class="esh-table">
    <p class="esh-link-wrapper">
        <a href="Catalog/Create" class="btn esh-button esh-button-primary">
            Create New
        </a>
    </p>

    @if (paginatedCatalogItems == null || !paginatedCatalogItems.Data.Any())
    {
        <table>
            <tr>
                <td>No data was returned.</td>
            </tr>
        </table>
    }
    else
    {
        <table class="table">
            <thead>
                <tr class="esh-table-header">
                    <th></th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Brand</th>
                    <th>Type</th>
                    <th>Price</th>
                    <th>Picture name</th>
                    <th>Stock</th>
                    <th>Restock</th>
                    <th>Max stock</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var catalogItem in paginatedCatalogItems.Data)
                {
                    <tr>
                        <td>
                            <img class="esh-thumbnail" src="@($"/Pics/{catalogItem.PictureFileName}")">
                        </td>
                        <td>
                            <p>@catalogItem.Name</p>
                        </td>
                        <td>
                            <p>@catalogItem.Description</p>
                        </td>
                        <td>
                            <p>@catalogItem.CatalogBrand.Brand</p>
                        </td>
                        <td>
                            <p>@catalogItem.CatalogType.Type</p>
                        </td>
                        <td>
                            <span class="esh-price">@catalogItem.Price</span>
                        </td>
                        <td>
                            <p>@catalogItem.PictureFileName</p>
                        </td>
                        <td>
                            <p>@catalogItem.AvailableStock</p>
                        </td>
                        <td>
                            <p>@catalogItem.RestockThreshold</p>
                        </td>
                        <td>
                            <p>@catalogItem.MaxStockThreshold</p>
                        </td>
                        <td>
                            <a href="@($"/Catalog/Edit/{catalogItem.Id}")" class="esh-table-link">
                                Edit
                            </a>
                            |
                            <a href="@($"/Catalog/Details/{catalogItem.Id}")" class="esh-table-link">
                                Details
                            </a>
                            |
                            <a href="@($"/Catalog/Delete/{catalogItem.Id}")" class="esh-table-link">
                                Delete
                            </a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
</div>

<div class="esh-pager">
    <div class="container">
        <article class="esh-pager-wrapper row">
            <nav>
                @if (pageIndex > 0)
                {
                    <a @onclick="Previous" class="esh-pager-item esh-pager-item--navigable">
                        Previous
                    </a>
                }

                <span class="esh-pager-item">
                    Showing @paginatedCatalogItems.ItemsPerPage of @paginatedCatalogItems.TotalItems products - Page @(paginatedCatalogItems.ActualPage + 1) - @paginatedCatalogItems.TotalPages
                </span>

                @if (pageIndex < paginatedCatalogItems.TotalPages - 1)
                {
                    <a @onclick="Next" class="esh-pager-item esh-pager-item--navigable">
                        Next
                    </a>
                }
            </nav>
        </article>
    </div>
</div>

@code {
    int pageSize = 10;
    int pageIndex = 0;
    PaginatedItemsViewModel<CatalogItem> paginatedCatalogItems;

    protected override void OnParametersSet() => LoadPage();

    void Previous()
    {
        pageIndex--;
        LoadPage();
    }

    void Next()
    {
        pageIndex++;
        LoadPage();
    }

    void LoadPage()
    {
        paginatedCatalogItems = CatalogService.GetCatalogItemsPaginated(pageSize, pageIndex);
    }
}
